﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace GameServer
{
    // 活动管理类
    public class ActivityManager : GameThreadObject
    {
        /// <summary>
        /// 保存活动列表的哈希表
        /// </summary>
        private static Hashtable _act = new Hashtable();

        /// <summary>
        /// 初始化函数，从配置文件中读取活动配置
        /// </summary>
        /// <returns>初始化结果</returns>
        public bool Init()
        {
            // todo:从配置文件中加载活动配置

            // 将活动配置和列表添加到哈希表中
            IActivity act = ActivityFactory.Create("TestActivity", 1, "测试活动");
            lock (_act.SyncRoot)
            {
                _act.Add(1, act);
            }

            return true;
        }

        /// <summary>
        /// 重载接口的Run函数
        /// </summary>
        public override void Run()
        {
            // todo:进行消息处理(message.Progress)
        }

        /// <summary>
        /// 重载接口的GameSecLoop函数
        /// </summary>
        public override void GameSecLoop()
        {
            // 将活动列表加载到中间变量中
            List<IActivity> actList = new List<IActivity>();
            lock (_act.SyncRoot)
            {
                foreach (IActivity act in _act.Values)
                {
                    actList.Add(act);
                }
            }

            // 驱动每个活动的GameSecLoop函数
            foreach (IActivity act in actList)
            {
                if (act != null)
                {
                    act.GameSecLoop();
                }
            }
        }

        private void GameMinuteLoop()
        {
            Console.WriteLine("每分执行一次:{0}", Global.DateTime);
        }
        private void GameHourLoop()
        {
            Console.WriteLine("每小时执行一次:{0}", Global.DateTime);
        }
        private void GameDayLoop()
        {
            Console.WriteLine("每天执行一次:{0}", Global.DateTime);
        }
    }

    /// <summary>
    /// 活动工厂类
    /// </summary>
    public class ActivityFactory
    {
        public static IActivity Create(String typeName, Int32 sn, String name)
        {
            Type type = Type.GetType("GameServer." + typeName);
            return Activator.CreateInstance(type, sn, name) as IActivity;
        }
    }
}
